﻿@model MatchSearchResultViewModel

@{ var routeValues = new { area = "MatchArea", id = Model.Match.Pk };}

@if (Model.IsVisible)
{
    <section>

        <div class="grid_6">
            <div>
                <a class="search1" href="@Url.Action("Index", "Details", new { area = "CompetitionArea", id = Model.Match.CompetitionGuid })">@Model.Match.Competition.CompetitionName</a>
            </div>

            <div>@Model.Match.MatchDate._ToDisplayString()</div>

            <div>
                <a class="search2" href="@Url.Action("Index", "Details", new { area = "VenueArea", id = Model.Match.VenueGuid })">@Model.Match.Venue.VenueName</a>
            </div>

            <div>@string.Format("{0:N}", Model.Match.Attendance)</div>
        </div>

        <div class="grid_6 clearfix">
            <div class="rtl">
                @if (Model.IsEditVisible) { @Html.Partial("Links/_EditLink", routeValues); }

                @if (Model.IsDeleteVisible) { @Html.Partial("Links/_DeleteLink", routeValues); }

                @if (Model.IsViewApprovalsVisible) { @Html.Partial("Links/_RecordForApprovalsLink", routeValues); }
            </div>
        </div>

        <div class="grid_12 clearfix"><hr /></div>

        <div class="grid_12 clearfix">

            <table style="font-size:1.2em;">
                <thead>
                    <tr>
                        <th class="rtl">@Model.Match.TeamVersion1.TeamName</th>
                        <th class="ctl">@string.Format("{0}-{1}", Model.Match.Team1FT, Model.Match.Team2FT)</th>
                        <th>@Model.Match.TeamVersion2.TeamName</th>
                    </tr>
                    <tr>
                        <td class="rtl">
                            @foreach (var item in Model.Match.MatchEvents.Where(m => m.MatchEventType == MatchEventType.Started && m.TeamGuid == Model.Match.Team1Guid).OrderBy(m => m.Minute).ThenBy(m => m.Extra))
                            {
                                <div>
                                    @foreach (var eventItem in Model.Match.MatchEvents.Where(m => m.PersonGuid == item.PersonGuid))
                                    {
                                        @Html.DisplayFor(d => eventItem);
                                    }
                                    <span>
                                        <a href="@Url.Action("Index", "Details", new { area = "PersonArea", id = item.PersonGuid })">@item.Person.FullName</a>
                                    </span>
                                </div>       
                            }
                        </td>
                        <td>&nbsp;</td>
                        <td>
                            @foreach (var item in Model.Match.MatchEvents.Where(m => m.MatchEventType == MatchEventType.Started && m.TeamGuid == Model.Match.Team2Guid))
                            {
                                <div>
                                    <a href="@Url.Action("Index", "Details", new { area = "PersonArea", id = item.PersonGuid })">@item.Person.FullName</a>

                                    @foreach (var eventItem in Model.Match.MatchEvents.Where(m => m.PersonGuid == item.PersonGuid))
                                    {
                                        @Html.DisplayFor(d => eventItem);
                                    }
                                </div>       
                            }
                        </td>
                    </tr>

                    <tr>
                        <td><hr /></td>
                        <td>&nbsp;</td>
                        <td><hr /></td>
                    </tr>

                    <tr>
                        <td class="rtl">
                            @foreach (var item in Model.Match.MatchEvents.Where(m => m.MatchEventType == MatchEventType.Substitute && m.TeamGuid == Model.Match.Team1Guid))
                            {
                                <div>
                                    @foreach (var eventItem in Model.Match.MatchEvents.Where(m => m.PersonGuid == item.PersonGuid))
                                    {
                                        @Html.DisplayFor(d => eventItem);
                                    }
                                    <a href="@Url.Action("Index", "Details", new { area = "PersonArea", id = item.PersonGuid })">@item.Person.FullName</a>
                                </div>       
                            }
                        </td>
                        <td>&nbsp;</td>
                        <td>
                            @foreach (var item in Model.Match.MatchEvents.Where(m => m.MatchEventType == MatchEventType.Substitute && m.TeamGuid == Model.Match.Team2Guid))
                            {
                                <div>
                                    <a href="@Url.Action("Index", "Details", new { area = "PersonArea", id = item.PersonGuid })">@item.Person.FullName</a>

                                    @foreach (var eventItem in Model.Match.MatchEvents.Where(m => m.PersonGuid == item.PersonGuid))
                                    {
                                        @Html.DisplayFor(d => eventItem);
                                    }
                                </div>       
                            }
                        </td>
                    </tr>
                </thead>
            </table>
        </div>
    </section>
}